package com.pm.megapinhata.client.view.models.impl;


import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.pm.megapinhata.client.AppController;
import com.pm.megapinhata.client.constants.NavigationContext;
import com.pm.megapinhata.client.presenter.LayoutBasicPresenter.Display;
import com.pm.megapinhata.client.view.models.LayoutAbstractMiddleView;
import com.pm.megapinhata.client.view.models.LayoutAbstractPrincipalView;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.widgets.Img;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.Layout;
import com.smartgwt.client.widgets.layout.VLayout;

public class LayoutBasicView extends LayoutAbstractPrincipalView implements Display {

	private VLayout principalLayout; // responsavel por conter todos os H's e V's layouts
	private HLayout cabecalhoLayout; // responsavel por conter o cabeçalho
	private HLayout rodapeLayout;    // resposavel por conter o rodapé
	private VLayout conteudoLayout;  //Contém o middleLayout
	private LayoutAbstractMiddleView<Layout> middleLayout; // responsavel por conter os componentes entre o cabeçalho e o rodapé
	private VLayout menuLayout;//Menu lateral
	public Label navegacaoLabel;
	
	public LayoutBasicView(LayoutAbstractMiddleView<Layout> middleLayout, VLayout menuLayout) {
		this.menuLayout = menuLayout;
		montaView(middleLayout);
		setNavegacaoLabel(NavigationContext.HOME);
	}

	@Override
	protected void montaView(LayoutAbstractMiddleView<Layout> layout) {
		this.middleLayout = layout;
		createPrincipalLayout();
	}

	private void createPrincipalLayout() {
		principalLayout = new VLayout();
		principalLayout.setWidth100();
//		principalLayout.setTop(100);
		principalLayout.addMember(createMenuContextoLayout());
		principalLayout.addMember(createRodapeLayout());
		principalLayout.setMargin(0);
		RootPanel.get().clear();
		RootPanel.get().add(principalLayout);
	}

	/**
	 * Metodo não está mais sendo utilizado pois o cabeçalho foi construido na própria página html.
	 * @return
	 */
	@Deprecated
	private HLayout createCabecalhoLayout() {
		cabecalhoLayout = new HLayout();
		cabecalhoLayout.setWidth100();
		cabecalhoLayout.setHeight("100");
		cabecalhoLayout.setBackgroundColor("blue");
		Label titleLabel = new Label("MEGAPINHATA");
		titleLabel.setAlign(Alignment.CENTER);
		cabecalhoLayout.addChild(titleLabel);
		//cabecalhoLayout.setBackgroundImage("logNormatelV2.png");
		//cabecalhoLayout.setBackgroundRepeat(BkgndRepeat.REPEAT_X);
		//image = new Img("imgNormatel.png",240,85);
		//cabecalhoLayout.addChild(image);
		return cabecalhoLayout;
	}

	private HLayout createRodapeLayout() {
		rodapeLayout = new HLayout();
		rodapeLayout.setWidth100();
		rodapeLayout.setHeight(25);
		rodapeLayout.setAlign(Alignment.CENTER);
		rodapeLayout.setBackgroundColor("#E6E6E6");
		Label rodapeLabel = new Label(AppController.messages.copyright());
		rodapeLabel.setWidth("1170");
		rodapeLabel.setAlign(Alignment.CENTER);
		rodapeLayout.addMember(rodapeLabel);
		return rodapeLayout;
	}

	private HLayout createMenuContextoLayout(){
		HLayout menuContextoLayout = new HLayout();
		menuContextoLayout.setHeight(510);
		
		menuLayout.setWidth(175);
		menuLayout.setHeight100();
		menuLayout.setBackgroundColor("#E6E6E6");
		
		menuContextoLayout.addMember(menuLayout);
		menuContextoLayout.addMember(createMiddleLayout());
		return menuContextoLayout;
	}
	
	private VLayout createMiddleLayout() {
		middleLayout.mountComponentView();
		conteudoLayout = new VLayout();
		conteudoLayout.addMember(middleLayout.getLayoutMiddleView());
		VLayout contextoLayout = new VLayout();
		contextoLayout.addMember(createNavegacaoLaytou());
		contextoLayout.addMember(conteudoLayout);
		return contextoLayout;
	}
	
	private HLayout createNavegacaoLaytou(){
		HLayout navegacaoLayout = new HLayout();
		navegacaoLayout.setHeight("25");
		navegacaoLayout.setWidth100();
		navegacaoLayout.setBackgroundColor("#E6E4E4");
		navegacaoLabel = new Label();
		navegacaoLabel.setMargin(5);
		navegacaoLabel.setWidth("995");
		VLayout sairLayout = new VLayout();
		sairLayout.setAlign(Alignment.CENTER);
		sairLayout.setMargin(5);
		navegacaoLayout.addMember(navegacaoLabel);
		return navegacaoLayout;
	}

	public LayoutAbstractMiddleView<Layout> getMiddleLayout() {
		return middleLayout;
	}

	public VLayout getPrincipalLayout() {
		return principalLayout;
	}

	public VLayout getMenuLayout() {
		return menuLayout;
	}
	
	public void alteraConteudoCentral(LayoutAbstractMiddleView<Layout> middleLayout, NavigationContext navegacaoContexto){
		conteudoLayout.removeMember(this.middleLayout.getLayoutMiddleView());
		this.middleLayout = middleLayout;
		this.middleLayout.mountComponentView();
		conteudoLayout.addMember(this.middleLayout.getLayoutMiddleView());
		setNavegacaoLabel(navegacaoContexto);
	}
	
	private void setNavegacaoLabel(NavigationContext navegacao) {
		if (NavigationContext.HOME.equals(navegacao)) {
			NavigationContext.setNavigation(NavigationContext.HOME.getCurrentMenu());
			navegacaoLabel.setContents(NavigationContext.getNavigation());
		}else {
			navegacaoLabel.setContents(NavigationContext.getNavigation() + " >> " + navegacao.getParentMenu() + " >> " + navegacao.getCurrentMenu());
		}
	}
	
	public Widget asWidget() {
		return this;
	}
}
